Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

CSS loader parsing CSS to JSON, platform suffix resolver and platform file system plugins #290

Merged
merged 1 commit into from
Nov 9, 2017

Conversation

PanayotCankov
Copy link
Contributor

@PanayotCankov PanayotCankov commented Oct 11, 2017

css2json-loader

The config can now be set to resolve .css files as:

module: {
  rules: [
    { test: /\.html$|\.xml$/, loader: "raw-loader" },
    { test: /\.css$/, loader: "nativescript-dev-webpack/css2json-loader" }
  ]
}

This will use the parser from tns-core-modules/css to parse the CSS content and convert it to a module that exports a JSON. The JSON generated is quite verbose so times should be carefully measured on real devices whether it is better to use this approach. For example our CSS parser may be able to parse the CSS theme faster than the JS module is loaded parsed etc.

PlatformSuffixPluing

The PlatformSuffixPluing is used like:

const PlatformSuffixPlugin = require("nativescript-dev-webpack/PlatformSuffixPlugin");
// ...
resolve: {
  plugins: [new PlatformSuffixPlugin(platform, ["ios", "android"])]
}

When a 'file' is resolved it checks if on the filesystem a file with a .platofrm. suffix exists, and if it does, resolves to it, otherwise resolves to the original file. This should eliminate the need to register extensions like [".android.ts", ".ts", ".android.css", ".css"] and use just [".ts", ".css"]. I've tested the approach in .js apps, we should further test this with Angular and TypeScript apps.

PlatformFSPlugin

This plugin is applied by adding:

        new PlatformFSPlugin({
            platform,
            platforms: ["android", "ios"],
            ignore: ["App_Resources"]
        })

This plugin is a preferred approach to the PlatformSuffixPluing as it will work on file system level, it will virtually filter the files from other platforms, and rename the files from the current platform. E.g. on the following tree:

app/
  app.css
  platform.android.css
  platform.ios.css

The webpack's compiler.inputFileSystem will map readdir, readFile, readJson etc. to behave for android as:

app/
  app.css
  platform.css (readFile will return the platform.android.css content for this one)

angular tooling with the RC works with this fs. Further the extensions doesn't need to be provided with [".android.ts", ".ts", ".android.css", ".css"] suffixes but use [".ts", ".css"] instead.

This also makes using web pack contexts easier, since when the files are enumerated their names will be properly mapped, for example:

function loadCss(path) { return require("~/" + path + ".css"); }

Will create internal map for app and platform to resolve to app/app.css and the app/platform.android.css, this can be used to express the vanilla {N} apps convention and put all CSS, XML and .ts/.js files within the app folder in such contexts.

It should be possible to use it to create a project with shared web, ios and android modules.

@PanayotCankov
Copy link
Contributor Author

run ci

@PanayotCankov
Copy link
Contributor Author

P.S. Registering global.registerModule("app.css", () => require("~/app.css")) and using the raw-loader for CSS files will work with the next release modules.

Copy link
Contributor

@vakrilov vakrilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we now modify the webapck config templates so that they use the plugin instead of resolve/extensions?

@@ -0,0 +1,41 @@
console.log("Here: " + __dirname);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably leftover console.log

.map(importRule => importRule.import.replace(/[\'\"]/gm, ""));
}

// Identity loader
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a leftover comment :)


PlatformSuffixPlugin.prototype.apply = function(resolver) {
var platform = this.platform;
var platforms = this.platforms;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

platforms is not used anywhere. Probably not needed?

Copy link
Contributor

@sis0k0 sis0k0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move the plugin to the plugins/ dir and export it from plugins/index.js. The plugins are then reexported from the root index.js and can be required with "nativescript-dev-webpaxk/SomePlugin". Maybe we should do the same for the loaders.

@PanayotCankov PanayotCankov changed the title CSS loader parsing CSS to JSON and platform suffix file resolver plugin CSS loader parsing CSS to JSON, platform suffix resolver and platform file system plugins Oct 17, 2017
@sis0k0 sis0k0 mentioned this pull request Nov 3, 2017
4 tasks
@SvetoslavTsenov
Copy link
Contributor

ci

@SvetoslavTsenov
Copy link
Contributor

testsappng

@SvetoslavTsenov
Copy link
Contributor

sdk

@sis0k0
Copy link
Contributor

sis0k0 commented Nov 8, 2017

run ci

@PanayotCankov PanayotCankov merged commit ea29bb6 into master Nov 9, 2017
@sis0k0 sis0k0 deleted the css2json-loader branch February 16, 2018 12:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants