Skip to content

Commit

Permalink
support custom web modules dir (#483)
Browse files Browse the repository at this point in the history
* adds support for mount:web_modules script

* adds tests for custom web_modules mount scripts

* adds comment to explain mount:web_modules args
  • Loading branch information
danprince committed Jun 12, 2020
1 parent cfebcbf commit 711a816
Show file tree
Hide file tree
Showing 21 changed files with 415 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/commands/build.ts
Expand Up @@ -228,6 +228,10 @@ export async function command(commandOptions: CommandOptions) {
}

const allBuiltFromFiles = new Set<string>();

const webModulesScript = config.scripts.find(script => script.id === 'mount:web_modules');
const webModulesLoc = webModulesScript ? webModulesScript.args.toUrl as string : '/web_modules';

for (const workerConfig of relevantWorkers) {
const {id, match, type} = workerConfig;
if (type !== 'build' || match.length === 0) {
Expand Down Expand Up @@ -317,7 +321,7 @@ export async function command(commandOptions: CommandOptions) {
if (dependencyImportMap.imports[spec]) {
let resolvedImport = path.posix.join(
config.buildOptions.baseUrl,
`/web_modules`,
webModulesLoc,
dependencyImportMap.imports[spec],
);
const extName = path.extname(resolvedImport);
Expand All @@ -337,7 +341,7 @@ export async function command(commandOptions: CommandOptions) {
pkgName: missingPackageName,
},
});
return path.posix.join(config.buildOptions.baseUrl, `/web_modules`, `${spec}.js`);
return path.posix.join(config.buildOptions.baseUrl, webModulesLoc, `${spec}.js`);
});
code = wrapImportMeta({code, env: true, hmr: false, config});
}
Expand Down
7 changes: 5 additions & 2 deletions src/commands/dev.ts
Expand Up @@ -286,6 +286,9 @@ export async function command(commandOptions: CommandOptions) {
filesBeingBuilt.delete(fileLoc);
}
}
const webModulesScript = config.scripts.find(script => script.id === 'mount:web_modules');
const webModulesLoc = webModulesScript ? webModulesScript.args.toUrl : '/web_modules';

const ext = path.extname(fileLoc).substr(1);
if (ext === 'js' || srcFileExtensionMapping[ext] === 'js') {
let missingWebModule: {spec: string; pkgName: string} | null = null;
Expand Down Expand Up @@ -318,7 +321,7 @@ export async function command(commandOptions: CommandOptions) {
}
if (dependencyImportMap.imports[spec]) {
let resolvedImport = path.posix.resolve(
`/web_modules`,
webModulesLoc,
dependencyImportMap.imports[spec],
);
const extName = path.extname(resolvedImport);
Expand All @@ -342,7 +345,7 @@ export async function command(commandOptions: CommandOptions) {
if (extName && extName !== '.js') {
spec = spec + '.proxy';
}
return `/web_modules/${spec}.js`;
return path.posix.join(webModulesLoc, `${spec}.js`);
});

messageBus.emit('MISSING_WEB_MODULE', {
Expand Down
14 changes: 10 additions & 4 deletions src/config.ts
Expand Up @@ -316,6 +316,7 @@ function handleLegacyProxyScripts(config: any) {

type RawScripts = Record<string, string>;
function normalizeScripts(cwd: string, scripts: RawScripts): BuildScript[] {
const dependenciesLoc = process.env.NODE_ENV === 'production' ? BUILD_DEPENDENCIES_DIR : DEV_DEPENDENCIES_DIR;
const processedScripts: BuildScript[] = [];
if (Object.keys(scripts).filter((k) => k.startsWith('bundle:')).length > 1) {
handleConfigError(`scripts can only contain 1 script of type "bundle:".`);
Expand Down Expand Up @@ -354,8 +355,15 @@ function normalizeScripts(cwd: string, scripts: RawScripts): BuildScript[] {
`scripts[${scriptId}]: "--to ${to}" must be a URL path, and start with a "/"`,
);
}
const dirDisk = cmdArr[0];
let dirDisk = cmdArr[0];
const dirUrl = to || `/${cmdArr[0]}`;

// mount:web_modules is a special case script where the fromDisk
// arg is harcoded to match the internal dependency dir
if (scriptId === 'mount:web_modules') {
dirDisk = dependenciesLoc;
}

newScriptConfig.args = {
fromDisk: path.posix.normalize(dirDisk + '/'),
toUrl: path.posix.normalize(dirUrl + '/'),
Expand All @@ -379,15 +387,13 @@ function normalizeScripts(cwd: string, scripts: RawScripts): BuildScript[] {
}

if (!scripts['mount:web_modules']) {
const fromDisk =
process.env.NODE_ENV === 'production' ? BUILD_DEPENDENCIES_DIR : DEV_DEPENDENCIES_DIR;
processedScripts.push({
id: 'mount:web_modules',
type: 'mount',
match: ['web_modules'],
cmd: `mount $WEB_MODULES --to /web_modules`,
args: {
fromDisk,
fromDisk: dependenciesLoc,
toUrl: '/web_modules',
},
});
Expand Down
1 change: 1 addition & 0 deletions test/build/custom-modules-dir/.gitignore
@@ -0,0 +1 @@
build
19 changes: 19 additions & 0 deletions test/build/custom-modules-dir/index.test.js
@@ -0,0 +1,19 @@
const fs = require('fs');
const path = require('path');
const execa = require('execa');

it('custom web_modules folder', () => {
execa.sync('npm', ['run', 'TEST'], {
cwd: __dirname,
// override NODE_ENV=test from jest, otherwise snowpack will assume
// development mode and try to copy from DEV_DEPENDENCIES_DIR
env: { NODE_ENV: 'production' }
});

// should write modules to the custom folder on disk
expect(fs.existsSync(path.join(__dirname, 'build', 'my_modules', 'array-flatten.js'))).toBe(true);

// should rewrite path for web_modules imports
const outputJS = fs.readFileSync(path.resolve(__dirname, 'build', '_dist_', 'index.js'), 'utf8');
expect(outputJS).toContain('/my_modules/array-flatten.js');
});
21 changes: 21 additions & 0 deletions test/build/custom-modules-dir/node_modules/array-flatten/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions test/build/custom-modules-dir/node_modules/array-flatten/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 711a816

Please sign in to comment.