Skip to content

Add a globals.registerWebpackModules that can register dynamic require webpack context #5087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2017

Conversation

PanayotCankov
Copy link
Contributor

The web pack require can now create a dynamic require context, and that context can be fed into the modules so the .js and .css files can be loaded through web pack loaders.

Consider the following bundle.config.js:

if (global.TNS_WEBPACK) {
    // registers tns-core-modules UI framework modules
    require("bundle-entry-points");

    // register application modules
    // This will register each xml, css, js, ts, scss etc. in the app/ folder
    const context = require.context("~/", true, /page\.(xml|css|js|ts|scss)$/);
    global.registerWebpackModules(context);
}

This will make a context for the entire app (~/ is the app/) folder and recursively require all files that end in ...page.xml, ...page.css, ...page.ts etc. Webpack will create a map such as:

var map = {
	"./main-page.js": 151,
	"./main-page.scss": 153,
	"./main-page.xml": 154,
	"./views/second-page.css": 155,
	"./views/second-page.js": 156,
	"./views/second-page.xml": 157
};

for the bundle.js, (note they can be added to the vendor.js and get into the snapshot for android too) and then when the {N} builder resolves a page, it will load the css and js files from that map.

The last argument of require.context is a regex so it allows great flexibility.
Developers will no longer need to global.registerModule for each of their JavaScript files as long as they can figure out a convention (like placing all views in a views folder, or postfixing each page with page.*, or if they are happy listing each .js file from app/.

This utility is designed to work with the nativescript-dev-webpack's PlatformFSPlugin or NativeScriptAngularCompilerPlugin that would virtually map file.android.* and file.ios.* to file.*.

@ghost ghost assigned PanayotCankov Nov 21, 2017
@ghost ghost added the in progress label Nov 21, 2017
@vakrilov
Copy link
Contributor

webpack-ftw

@PanayotCankov
Copy link
Contributor Author

Not all, at some point we will have to handle the "xml" files being added with the raw-loader... Then compiling the XML to JavaScript will be a matter of writing yet another loader that reads XMLs and writes JavaScript.

Copy link
Contributor

@hshristov hshristov left a comment

Choose a reason for hiding this comment

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

LGTM

@PanayotCankov PanayotCankov merged commit 255feda into master Nov 22, 2017
@PanayotCankov PanayotCankov deleted the register-webpack-context branch November 22, 2017 11:02
@lock
Copy link

lock bot commented Aug 26, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Aug 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants