Skip to content

Commit

Permalink
refactor(core): module -> {} in browser builds (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
AviVahl authored Sep 18, 2023
1 parent f3b4c33 commit f29c5c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "mocha \"./dist/test/**/*.spec.js\""
},
"browser": {
"module": "./dist/module-with-extensions.js"
"module": false
},
"dependencies": {
"@tokey/css-selector-parser": "^0.6.2",
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// in browser build this gets remapped to an empty object via our package.json->"browser"
import nodeModule from 'module';
// importing the factory directly, as we feed it our own fs, and don't want graceful-fs to be implicitly imported
// this allows @stylable/core to be bundled for browser usage without special custom configuration
import ResolverFactory from 'enhanced-resolve/lib/ResolverFactory.js';

import type { ModuleResolver } from './types';
import type { MinimalFS } from './cached-process-file';

function bundleSafeRequireExtensions(): string[] {
let extensions: string[];
try {
// we use eval here to avoid bundling warnings about require.extensions we always has fallback for browsers
extensions = Object.keys(require('module')._extensions);
// we use nodeModule here to avoid bundling warnings about require.extensions we always has fallback for browsers
extensions = Object.keys(
(nodeModule as typeof nodeModule & { _extensions?: Record<string, unknown> })
._extensions ?? {}
);
} catch (e) {
extensions = [];
}
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/module-with-extensions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/create-stylable-app/src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import yargs from 'yargs';
import { createProjectFromTemplate } from './create-project';

const argv = yargs
const argv = yargs()
.usage('npm init stylable-app <project-name>')
.demand(1, 'missing project-name')
.option('template', {
Expand Down

0 comments on commit f29c5c7

Please sign in to comment.