Skip to content
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

Fix module entry point by migrating to vite for ESM building #4276

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ module.exports = {
plugins: [
'cypress',
],
parserOptions: {
babelOptions: {
plugins: [
'@babel/plugin-syntax-import-assertions',
],
},
},
}
16 changes: 11 additions & 5 deletions resources/translations.js → build/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const { join, basename } = require('path')
const fs = require('fs/promises')
const gettextParser = require('gettext-parser')

// https://github.com/alexanderwallin/node-gettext#usage
// https://github.com/alexanderwallin/node-gettext#load-and-add-translations-from-mo-or-po-files
const parseFile = async (fileName) => {
// We need to import dependencies dynamically to support this module to be imported by vite and to be required by Cypress
// If we use require, vite will fail with 'Dynamic require of "path" is not supported'
// If we convert it to an ES module, webpack and vite are fine but Cypress will fail because it can not handle ES imports in Typescript configs in commonjs packages
const { basename } = await import('path')
const { readFile } = await import('fs/promises')
const gettextParser = await import('gettext-parser')

const locale = basename(fileName).slice(0, -'.pot'.length)
const po = await fs.readFile(fileName)
const po = await readFile(fileName)

const json = gettextParser.po.parse(po)

Expand Down Expand Up @@ -56,7 +60,9 @@ const parseFile = async (fileName) => {
}

const loadTranslations = async (baseDir) => {
const files = await fs.readdir(baseDir)
const { join } = await import('path')
const { readdir } = await import('fs/promises')
const files = await readdir(baseDir)

const promises = files
.filter(name => name !== 'messages.pot' && name.endsWith('.pot'))
Expand Down
4 changes: 2 additions & 2 deletions build/usernameToColor-export.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const snapshot = require('./../src/functions/usernameToColor/__snapshots__/usernameToColor.spec.js.snap')
const result = {}
Object.keys(snapshot).map((key) => {
const uid = key.replace('usernameToColor ', '').replace(' has the proper color 1', '')
result[uid] = JSON.parse(snapshot[key])
const uid = key.replace('usernameToColor ', '').replace(' has the proper color 1', '')
result[uid] = JSON.parse(snapshot[key])
})
console.log(JSON.stringify(result))
15 changes: 8 additions & 7 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { defineConfig } from 'cypress'
import { DefinePlugin } from 'webpack'
import getCompareSnapshotsPlugin from 'cypress-visual-regression/dist/plugin'
import webpack from 'webpack'
import getCompareSnapshotsPlugin from 'cypress-visual-regression/dist/plugin.js'
import path from 'path'
import webpackConfig from '@nextcloud/webpack-vue-config'
import webpackRules from '@nextcloud/webpack-vue-config/rules'
import webpackRules from '@nextcloud/webpack-vue-config/rules.js'

import { loadTranslations } from './resources/translations'
import { loadTranslations } from './build/translations.js'

const SCOPE_VERSION = Date.now()
webpackRules.RULE_SCSS.use.push({
const SCOPE_VERSION = Date.now();

(webpackRules.RULE_SCSS.use as webpack.RuleSetUse[]).push({
loader: 'sass-loader',
options: {
additionalData: `@use 'sass:math'; $scope_version:${SCOPE_VERSION}; @import 'variables'; @import 'material-icons';`,
Expand Down Expand Up @@ -72,7 +73,7 @@ export default defineConfig({
bundler: 'webpack',
webpackConfig: async () => {
const translations = await loadTranslations(path.resolve(__dirname, './l10n'))
webpackConfig.plugins.push(new DefinePlugin({
webpackConfig.plugins.push(new webpack.DefinePlugin({
PRODUCTION: false,
SCOPE_VERSION,
TRANSLATIONS: JSON.stringify(translations),
Expand Down
2 changes: 1 addition & 1 deletion cypress/visual/NcAppSidebar/NcAppSidebar-compact.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import BuildAppSidebarTest from './NcAppSidebarMixin'
import BuildAppSidebarTest from './NcAppSidebarMixin.js'

BuildAppSidebarTest(true)
2 changes: 1 addition & 1 deletion cypress/visual/NcAppSidebar/NcAppSidebar.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import BuildAppSidebarTest from './NcAppSidebarMixin'
import BuildAppSidebarTest from './NcAppSidebarMixin.js'

BuildAppSidebarTest(false)
Loading